package sdmakerz_v1.coresyntax.generator;

import com.google.common.base.Objects;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import sdmakerz_v1.coresyntax.SDMakerV1Utils;
import sdmakerz_v1.coresyntax.generator.MethodDataImpl;
import sdmakerz_v1.coresyntax.generator.interfaces.IInputData;
import sdmakerz_v1.coresyntax.generator.interfaces.IInterfaceData;
import sdmakerz_v1.coresyntax.generator.interfaces.IMethodData;
import sdmakerz_v1.coresyntax.sDMakerV1.Call;
import sdmakerz_v1.coresyntax.sDMakerV1.Diagram;
import sdmakerz_v1.coresyntax.sDMakerV1.Obj;

/**
 * Une classe qui simplfie la recuperation des donnees d'une Interface
 * et de ses instances.
 * @author pstl-team
 */
@SuppressWarnings("all")
public class InterfaceDataImpl implements IInterfaceData {
  /**
   * Le nom de l'interface
   */
  private String name;
  
  /**
   * L'identifiant unique de l'ObjectClass representant cette
   *  interface
   */
  private String interfaceID;
  
  /**
   * les noms des instances existantes du type
   */
  private List<Obj> objs;
  
  private ArrayList<IMethodData> methodsDatas;
  
  /**
   * ** INITIALIZERS **
   */
  public void init() {
    ArrayList<Obj> _arrayList = new ArrayList<Obj>();
    this.objs = _arrayList;
    ArrayList<IMethodData> _arrayList_1 = new ArrayList<IMethodData>();
    this.methodsDatas = _arrayList_1;
  }
  
  /**
   * ** SETTERS **
   */
  public IInterfaceData setName(final String name) {
    this.name = name;
    return this;
  }
  
  public void setInterfaceID(final String interfaceID) {
    this.interfaceID = interfaceID;
  }
  
  public void addObj(final Obj obj) {
    this.objs.add(obj);
  }
  
  public void addCall(final IInputData inputData, final Call c) {
    List<IMethodData> matchingMethods = this.matchingMethods(inputData, c);
    int _size = matchingMethods.size();
    boolean _equals = (_size == 0);
    if (_equals) {
      MethodDataImpl _methodDataImpl = new MethodDataImpl();
      IMethodData methodData = _methodDataImpl;
      methodData.init(inputData, c);
      methodData.addCall(inputData, c);
      this.methodsDatas.add(methodData);
      matchingMethods.add(methodData);
    }
    for (final IMethodData methodData_1 : matchingMethods) {
      methodData_1.addCall(inputData, c);
    }
  }
  
  /**
   * ** GETTERS **
   */
  public String getInterfaceID() {
    return this.interfaceID;
  }
  
  public String getQualifiedName() {
    return this.name;
  }
  
  public IMethodData getMethodData(final IInputData inputData, final Call c) {
    IMethodData methodData = null;
    List<IMethodData> matchingMethod = this.matchingMethods(inputData, c);
    int _size = matchingMethod.size();
    boolean _equals = (_size == 1);
    if (_equals) {
      IMethodData _get = matchingMethod.get(0);
      methodData = _get;
    }
    return methodData;
  }
  
  public List<IMethodData> getAllMethodData() {
    return this.methodsDatas;
  }
  
  private List<IMethodData> matchingMethods(final IInputData inputData, final Call c) {
    ArrayList<IMethodData> _arrayList = new ArrayList<IMethodData>();
    List<IMethodData> matchingMethod = _arrayList;
    for (final IMethodData methodData : this.methodsDatas) {
      boolean _match = methodData.match(inputData, c);
      if (_match) {
        matchingMethod.add(methodData);
      }
    }
    return matchingMethod;
  }
  
  /**
   * ** TESTORS **
   */
  public Boolean contains(final Obj obj) {
    boolean contains = false;
    for (final Obj objTmp : this.objs) {
      {
        Resource _eResource = obj.eResource();
        URI _uRI = _eResource.getURI();
        String strURI = _uRI.toString();
        Resource _eResource_1 = objTmp.eResource();
        URI _uRI_1 = _eResource_1.getURI();
        String strURITmp = _uRI_1.toString();
        EObject eo = SDMakerV1Utils.findContainer(obj, Diagram.class);
        EObject eoTmp = SDMakerV1Utils.findContainer(objTmp, Diagram.class);
        Diagram d = ((Diagram) eo);
        Diagram dTmp = ((Diagram) eoTmp);
        boolean _and = false;
        boolean _and_1 = false;
        boolean _notEquals = (!Objects.equal(strURI, null));
        if (!_notEquals) {
          _and_1 = false;
        } else {
          boolean _notEquals_1 = (!Objects.equal(strURITmp, null));
          _and_1 = (_notEquals && _notEquals_1);
        }
        if (!_and_1) {
          _and = false;
        } else {
          int _compareTo = strURI.compareTo(strURITmp);
          boolean _equals = (_compareTo == 0);
          _and = (_and_1 && _equals);
        }
        if (_and) {
          boolean _and_2 = false;
          boolean _and_3 = false;
          boolean _notEquals_2 = (!Objects.equal(d, null));
          if (!_notEquals_2) {
            _and_3 = false;
          } else {
            boolean _notEquals_3 = (!Objects.equal(dTmp, null));
            _and_3 = (_notEquals_2 && _notEquals_3);
          }
          if (!_and_3) {
            _and_2 = false;
          } else {
            String _name = d.getName();
            String _name_1 = dTmp.getName();
            int _compareTo_1 = _name.compareTo(_name_1);
            boolean _equals_1 = (_compareTo_1 == 0);
            _and_2 = (_and_3 && _equals_1);
          }
          if (_and_2) {
            String _name_2 = obj.getName();
            String _name_3 = objTmp.getName();
            int _compareTo_2 = _name_2.compareTo(_name_3);
            boolean _equals_2 = (_compareTo_2 == 0);
            contains = _equals_2;
          }
        }
      }
    }
    return Boolean.valueOf(contains);
  }
}
